package com.lhb.SC.admin.feign.configuration;

import com.lhb.SC.admin.feign.codec.MonitorDecoder;
import com.lhb.SC.admin.feign.codec.MonitorEncoder;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import feign.optionals.OptionalDecoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@AutoConfigureAfter(FeignClientsConfiguration.class)
public class FeignClientsOverrideConfiguration {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    public Decoder feignDecoder() {
        return new OptionalDecoder(
                new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
    }

    @Bean
    public MonitorDecoder monitorFeignDecoder() {
        return new MonitorDecoder(feignDecoder());
    }


    public Encoder springFormEncoder() {
        return new SpringFormEncoder();
    }


    @Bean
    public MonitorEncoder monitorEncoder() {
        return new MonitorEncoder(springFormEncoder());
    }

}
